﻿using Jupiter.Infrastructure.Exceptions;
using Jupiter.Infrastructure.Permission.Attributes;
using Jupiter.Infrastructure.Responses;
using MediatR;
using Microsoft.AspNetCore.Mvc;
using Wips.AppService.Publisher.PublishFlows;
using Wips.AppService.Publisher.PublishFlows.Models;
using Wips.AppService.Publisher.PublishFlows.Requests;
using Wips.AppService.Publisher.PublishFlows.Responses;

namespace Wips.MainServer.Controllers
{
    /// <summary>
    /// 发布流程服务
    /// </summary>
    public class PublishFlowController : CustomeBaseController
    {
        /// <summary>
        /// 服务
        /// </summary>
        private readonly IPublishFlowAppService _publishFlowAppService;

        /// <summary>
        /// 操作
        /// </summary>
        public readonly IMediator _mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="publishFlowAppService"></param>
        /// <param name="mediator"></param>
        public PublishFlowController(IPublishFlowAppService publishFlowAppService, IMediator mediator)
        {
            _publishFlowAppService = publishFlowAppService;
            _mediator = mediator;
        }

        /// <summary>
        /// 获取注册的服务
        /// </summary>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<PageResult<GetPublishFlowPagesResponse>> GetPageAsync([FromBody] GetPublishFlowPagesRequest request)
        {
            return await _publishFlowAppService.GetPageAsync(request);
        }


        /// <summary>
        /// 获取发布流程详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Permission]
        [HttpGet]
        public async Task<PublishFlowModel> GetById(string id)
        {
            return await _publishFlowAppService.GetById(id);
        }

        /// <summary>
        /// 开始发布流程
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> StartPublishAsync([FromBody] StartPublishRequest request)
        {

            return await _mediator.Send(request);
        }

        /// <summary>
        /// 保存发布流程节点
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> SavePublishFlowNodeAsync([FromBody] SavePublishFlowNodeRequest request)
        {
            return await _mediator.Send(request);
        }


        /// <summary>
        /// 获取对应流程中所有zip文件的目录表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="CoreException"></exception>
        [Permission]
        [HttpGet]
        public async Task<List<GetFlowNodeZipFileListResponse>> GetFlowZipFileListAsync(string id)
        {
            return await _publishFlowAppService.GetFlowZipFileListAsync(id);
        }

        /// <summary>
        /// 确认发布
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> SurePublishAsync([FromBody] SurePublishRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 执行脚本
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<List<string>> CommandRunScriptAsync([FromBody] CommandRunScriptRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 取消/终止发布
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> CancelPublishAsync([FromBody] CancelPublishRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 还原备份
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> RestorePublishAsync([FromBody] RestorePublishRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 完成发布
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Permission]
        [HttpPost]
        public async Task<string> CompletePublishAsync([FromBody] CompletePublishRequest request)
        {
            return await _mediator.Send(request);
        }


        /// <summary>
        /// 获取日志列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Permission]
        [HttpGet]
        public async Task<List<PublishFlowLogModel>> GetLogListAsync(string id)
        {
            return await _publishFlowAppService.GetLogListAsync(id);
        }
    }
}
